package cn.zh.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

//@Configuration
public class RabbitDirectConfig {

    @Bean
    public Queue directQueue() {
        Map<String, Object> args = new HashMap<>(3);
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "dead_exchange");
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "dead");
        //声明队列的 TTL
        args.put("x-message-ttl", 10000);
        //设置队列优先级
        args.put("x-max-priority",5);
        //参数介绍
        //1.队列名 2.是否持久化 3.是否独占 4.自动删除 5.其他参数
        return new Queue("directQueue-One", false, false, false, args);
    }

    @Bean
    public Queue directQueue2() {
        Queue queue = QueueBuilder
                //持久化并设置队列名
                .durable("dis")
                //自动删除
                .autoDelete()
                //TTL时间
                .ttl(100)
                //死信交换机
                .deadLetterExchange("")
                //死信路由key
                .deadLetterRoutingKey("Dead")
                //开启队列优先级，并设置优先级数
                .maxPriority(5)
                //设为惰性队列
                .lazy()
                .build();
        return queue;
    }

    @Bean
    public Queue alternateQueue() {
        //参数介绍
        //1.队列名 2.是否持久化 3.是否独占 4.自动删除 5.其他参数
        Queue queue = QueueBuilder.durable("alternateQueue")
                .autoDelete()
                .build();
        return queue;
    }

    @Bean
    public FanoutExchange alternateExchange() {
        return new FanoutExchange("Alternate_Exchange", true, false, null);
    }

    @Bean
    public DirectExchange directExchange() {
        ExchangeBuilder exchange = ExchangeBuilder.directExchange("MqSendService-One")
                .durable(false)
                .autoDelete()
                .withArgument("alternate-exchange", "Alternate_Exchange");
        //参数介绍
        //1.交换器名 2.是否持久化 3.自动删除 4.其他参数
        Map<String, Object> args = new HashMap<>(3);
        args.put("alternate-exchange", "Alternate_Exchange");
        return new DirectExchange("MqSendService-One", false, false, args);
    }

    @Bean
    public Binding bingAlternateExchange() {
        //绑定队列
        return BindingBuilder.bind(alternateQueue())
                //队列绑定到哪个交换器
                .to(alternateExchange());
    }

    @Bean
    public Binding bingExchange() {
        //绑定队列
        return BindingBuilder.bind(directQueue())
                //队列绑定到哪个交换器
                .to(directExchange())
                //路由key,必须指定
                .with("One");
    }

    @Bean
    public Binding bingExchange2() {
        //绑定队列
        return BindingBuilder.bind(directQueue2())
                //队列绑定到哪个交换器
                .to(directExchange())
                //路由key,必须指定
                .with("Two");
    }

}
